package com.ztgf.shopping.api.cms.shop;

import com.qiyuesuo.sdk.v2.SdkClient;
import com.qiyuesuo.sdk.v2.request.ContractDownloadRequest;
import com.qiyuesuo.sdk.v2.utils.IOUtils;
import com.ztgf.common.constant.Result;
import com.ztgf.common.enums.CodeEnum;
import com.ztgf.shopping.models.bo.shop.ShopContractSettleInBO;
import com.ztgf.shopping.models.bo.shop.ShopContractSettleInUpdateBO;
import com.ztgf.shopping.models.dto.shop.ShopContractSettleInDTO;
import com.ztgf.shopping.models.service.shop.IShopContractSettleInService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * author: chenyu.wang
 * date: 2020-07-10 14:43
 * Description: 入驻合同控制器
 */
@RestController
@Slf4j
@RequestMapping("/cms/shopContractSettleIn")
@Api(value = "ShopContractSettleInController", tags = "入驻合同表")
public class ShopContractSettleInController {

    @Value("${shop.qiyuesuo.service.sdkUrl}")
    private String serverUrl;

    @Value("${shop.qiyuesuo.service.appToken}")
    private String accessKey;

    @Value("${shop.qiyuesuo.service.appSecret}")
    private String accessSecret;

    @DubboReference
    private IShopContractSettleInService shopContractSettleInService;

    @PostMapping("/queryAdminPageList")
    @ApiOperation(value = "后台管理系统入驻合同列表", notes = "后台管理系统入驻合同列表")
    public Result queryAdminPageList(ShopContractSettleInBO bo) {
        return new Result<>(shopContractSettleInService.queryAdminPageList(bo));
    }

    @PostMapping("/getById")
    @ApiOperation(value = "后台管理系统入驻合同查看详情", notes = "后台管理系统入驻合同查看详情")
    public Result getById(Long id) {
        if (id == null) {
            return new Result(CodeEnum.FAIL_100002);
        }
        return new Result<>(shopContractSettleInService.getById(id));
    }

    @PostMapping("/getByShopId")
    @ApiOperation(value = "开店时限", notes = "开店时限")
    @ApiImplicitParam(name = "shopId",value = "shopId",required = true,paramType = "query", dataType = "Long")
    public Result<ShopContractSettleInDTO> getByShopId(Long shopId) {
        if (shopId == null) {
            return new Result<>(CodeEnum.FAIL_100002);
        }
        return new Result<>(shopContractSettleInService.getByShopId(shopId));
    }

    @PostMapping("/updateByShopId")
    @ApiOperation(value = "到期续约", notes = "到期续约")
    public Result updateByShopId(ShopContractSettleInUpdateBO bo) {
        if (bo.getId() == null || null == bo.getEndDate()) {
            return new Result(CodeEnum.FAIL_100002);
        }

        ShopContractSettleInDTO dto = new ShopContractSettleInDTO();
        dto.setEndDate(bo.getEndDate());
        dto.setId(bo.getId());
        dto.setExpireStatus(0); // 到期状态 0正常，1即将到期，2已过期

        boolean flag = shopContractSettleInService.updateById(dto);
        if (flag){
            return new Result<>();
        }else{
            return new Result<>(CodeEnum.FAIL);
        }
    }

    @PostMapping("/getContactUrl")
    @ApiOperation(value = "查看合同",notes = "查看合同")
    @ApiImplicitParam(name = "contractId",value = "contractId 契约锁合同id",required = true,paramType = "query", dataType = "Long")
    public Result<String> getContactUrl(Long contractId){
        String contractString = shopContractSettleInService.getContractString(contractId);
        return new Result<>(contractString);
    }

    /**
     * 根据店铺id下载合同
     * @return
     */
    @GetMapping("/settleContractDownload/{shopId}")
    public void settleContractDownload(@PathVariable("shopId") Long shopId, HttpServletResponse response) throws IOException {
        response.setHeader("Content-Disposition", "attachment;fileName=" + shopId + ".zip");
        ShopContractSettleInDTO shopContractSettleInDTO = shopContractSettleInService.getByShopId(shopId);
        SdkClient sdkClient = new SdkClient(serverUrl, accessKey, accessSecret);
        // 下载合同
        ContractDownloadRequest request = new ContractDownloadRequest(Long.parseLong(shopContractSettleInDTO.getContractLockId()));
        List<String> str = new ArrayList<>();
        str.add("CONTRACT");
        request.setDownloadItems(str);
        log.info(request.getUrl());
        OutputStream fos = response.getOutputStream();
        //FileOutputStream fos = new FileOutputStream("E:/module/doc.zip");
        sdkClient.download(request, fos);
        IOUtils.safeClose(fos);
        log.info("下载合同成功");
    }
}
